package com.dongwu.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author zhanghui
 * @create 2016年12月15日 上午10:17:45
 */
public class DateUtils  {
	
	protected static Logger logger  =  LoggerFactory.getLogger(DateUtils.class);

	public static final DateFormat DATE_FORMAT = new SimpleDateFormat(
			"yyyy-MM-dd", java.util.Locale.CHINA);

	public static final DateFormat TIME_FORMAT = new SimpleDateFormat(
			"HH:mm:ss", java.util.Locale.CHINA);

	public static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss", java.util.Locale.CHINA);

	public static final DateFormat DATE_TIME_FORMAT_IMAGE = new SimpleDateFormat(
			"yyyyMMddHHmmss", java.util.Locale.CHINA);

	public static final String PATTERN_DATE = "yyyy-MM-dd";

	public static final String PATTERN_DATE_NO_HYPHEN = "yyyyMMdd";

	public static final String PATTERN_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

	public static final String PATTERN_DATE_MILLISECOND = "yyyy-MM-dd HH:mm:ss.SSS";

	public static final String PATTERN_MONTH = "yyyy-MM";

	/**
	 * 格式化当前日期
	 * 
	 * @param pattern
	 * @return
	 */
	public static String getCurrTimeStr(String pattern) {
		return formatDate(new Date(), pattern);
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formatDate(Date date, String pattern) {
		if (StringUtils.isBlank(pattern)) {
			return DATE_TIME_FORMAT.format(date);
		}
		DateFormat dateFormat = new SimpleDateFormat(pattern,
				java.util.Locale.CHINA);
		return dateFormat.format(date);
	}

	/**
	 * 昨天
	 * 
	 * @return
	 */
	public static Date getYesterday() {
		Date today = new Date();
//		Date yesterday = DateUtils.addDays(today, -1);
		return null;
	}

	/**
	 * 根据日期字符串解析得到date类型日期
	 * 
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String dateStr) throws ParseException {
		return parseDate(dateStr, PATTERN_DATE);
	}

	/**
	 * 根据日期字符串和日期格式解析得到date类型日期
	 * 
	 * @param dateStr
	 * @param datePattern
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String dateStr, String datePattern)
			throws ParseException {
		DateFormat format= new SimpleDateFormat(datePattern);
		return format.parse(dateStr);
		
	}

	/**
	 * 获取所传日期的本月第一天
	 * 
	 * @param dt
	 * @return
	 */
	public static String getFirstOfMonthDay(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = formatDate(c.getTime(), PATTERN_DATE);
		return firstDay;
	}

	/**
	 * 获取所传日期的本月最后一天
	 * 
	 * @param dt
	 * @return
	 */
	public static String getEndOfMonthDay(Date dt) {
		Calendar c = Calendar.getInstance();
		c.setTime(dt);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		String lastDay = formatDate(c.getTime(), PATTERN_DATE);
		return lastDay;
	}
	public static String getFirstOfMonthDay(String year,String month){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR,Integer.valueOf(year));
		c.set(Calendar.MONTH,Integer.valueOf(month)-1);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay =DateUtils.formatDate(c.getTime(),DateUtils.PATTERN_DATE);
		return firstDay;
	}
	public static String getEndOfMonthDay(String year,String month){
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR,Integer.valueOf(year));
		c.set(Calendar.MONTH,Integer.valueOf(month)-1);
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
		String firstDay =DateUtils.formatDate(c.getTime(),DateUtils.PATTERN_DATE);
		return firstDay;
	}
	
	/**
	 * 明天
	 * @return
	 */
	public static Date getTomorrow() {
		Date today = new Date();
//		Date tomorrow = DateUtils.addDays(today, +1);
		Date tomorrow =null;
		return tomorrow;
	}
	
	/**
	 * 	获取上月：
	 * @return
	 */

	public static String getLastMonth() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		SimpleDateFormat format =  new SimpleDateFormat("yyyyMM");
		String time = format.format(c.getTime());
	return time;
	}

	/**
	 * 返回当前时间 yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getCurrentTimeStr() {
		return DateUtils.formatDate(new Date(), DateUtils.PATTERN_DATE_TIME);
	}

	public static Date now() {
		return new Date();
	}
	
	
	public static boolean isInDate( Date date,Date start,  Date end)
	{
		final Date currentDate = new Date();
		if (null != start && currentDate.before(start))
		{
			return false;
		}
		if (null != end && currentDate.after(end))
		{
			return false;
		}
		return true;

	}
	/**
	 *  判断当前时间是否在某个时间段之内
	 * @param time
	 * @param from
	 * @param to
	 * @return
	 */
    public static boolean belongCalendarBetterwin(Date from, Date to) {
        Calendar date = Calendar.getInstance();
        
        Calendar after = Calendar.getInstance();
        after.setTime(from);

        Calendar before = Calendar.getInstance();
        before.setTime(to);

        if (date.after(after) && date.before(before)) {
            return true;
        } else {
            return false;
        }
    }
	/**
	 *  判断当前时间是不是在某个时间之前
	 * @param beforedate
	 * @return
	 */
    public static boolean belongCalendarBefore(Date beforedate) {
        Calendar date = Calendar.getInstance();

        Calendar before = Calendar.getInstance();
        before.setTime(beforedate);

        if (date.before(before)) {
            return true;
        } else {
            return false;
        }
    }
	/**
	 *  判断当前时间是不是在某个时间之后
	 * @param afterdate
	 * @return
	 */
    public static boolean belongCalendarAfter(Date afterdate) {
        Calendar date = Calendar.getInstance();

        Calendar after = Calendar.getInstance();
        after.setTime(afterdate);

        if (date.after(after)) {
            return true;
        } else {
            return false;
        }
    }
}
